package com.zjw;

import com.zjw.net.NetUtils;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

@SpringBootApplication
public class RemoteServerApplication {

    private static GraphicsDevice[] graphicsDevices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
    private static Robot robot;
    private static volatile int index = 0;

    static {
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }
    }

    private static BufferedImage screenCapture1 = null;
    private static BufferedImage screenCapture2 = null;
    private static LinkedList<BufferedImage> screenCaptures1 = new LinkedList<>();
    private static LinkedList<BufferedImage> screenCaptures2 = new LinkedList<>();

    public static void main(String[] args) throws AWTException, IOException {
        createUI();
//        new Thread(NetUtils::udpServer).start();
//        new SpringApplicationBuilder(RemoteServerApplication.class).headless(false).run(args);
    }

    public static void createUI() {
        JFrame frame = new JFrame();
        frame.setSize(1000, 1000);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(null);
        frame.setLocationRelativeTo(null);
        JPanel panel1 = new JPanel() {
            @Override
            public void paint(Graphics g) {
                if (screenCapture1 != null) {
                    g.drawImage(screenCapture1, 0, 0, (int) (500 * (16.0 / 9)), 500, null);
                }
            }
        };
        panel1.setSize((int) (500 * (16.0 / 9)), 500);
        panel1.setLocation(0, 0);
        JPanel panel2 = new JPanel() {
            @Override
            public void paint(Graphics g) {
                if (screenCapture2 != null) {
                    g.drawImage(screenCapture2, 0, 0, (int) (500 * (16.0 / 9)), 500, null);
                }
            }
        };
        panel2.setLocation(0, 500);
        panel2.setSize((int) (500 * (288.0 / 180)), 500);
        frame.add(panel1);
        frame.add(panel2);
        frame.setVisible(true);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                screenCapture1 = robot.createScreenCapture(graphicsDevices[index].getDefaultConfiguration().getBounds());
                screenCapture2 = robot.createScreenCapture(graphicsDevices[1].getDefaultConfiguration().getBounds());
                panel1.repaint();
                panel2.repaint();
            }
        }, 0, 10);
        panel1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (index < graphicsDevices.length - 1) {
                    index++;
                } else {
                    index = 0;
                }
                System.out.println(index);
            }
        });
        FFmpegFrameRecorder recorder = new FFmpegFrameRecorder("C:\\Users\\11011\\Desktop\\a.mp4", 1920, 1080);
        recorder.setVideoCodec(avcodec.AV_CODEC_ID_MPEG4);
        recorder.setFormat("mp4");
        recorder.setSampleRate(44100);
        recorder.setFrameRate(5);
        recorder.setVideoQuality(0);
        recorder.setVideoOption("crf","23");
        // 2000 kb/s, 720P视频的合理比特率范围
        recorder.setVideoBitrate(1000000);
        /**
         * 权衡quality(视频质量)和encode speed(编码速度) values(值)&#xff1a; ultrafast(终极快),superfast(超级快),
         * veryfast(非常快), faster(很快), fast(快), medium(中等), slow(慢), slower(很慢),
         * veryslow(非常慢)
         * ultrafast(终极快)提供最少的压缩&#xff08;低编码器CPU&#xff09;和最大的视频流大小&#xff1b;而veryslow(非常慢)提供最佳的压缩&#xff08;高编码器CPU&#xff09;的同时降低视频流的大小
         * 参考&#xff1a;https://trac.ffmpeg.org/wiki/Encode/H.264 官方原文参考&#xff1a;-preset ultrafast as the
         * name implies provides for the fastest possible encoding. If some tradeoff
         * between quality and encode speed, go for the speed. This might be needed if
         * you are going to be transcoding multiple streams on one machine.
         */

    }

}
